package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.PackMaintain;
import com.spice.service.mapper.PackMaintainMapper;
import com.spice.service.service.PackMaintainService;
import org.springframework.stereotype.Service;

@Service
public class PackMaintainServiceImpl extends ServiceImpl<PackMaintainMapper, PackMaintain> implements PackMaintainService {
    @Override
    public Response savePackMaintain(PackMaintain newPackMaintain) {
    //确认必要参数
        if (CheckUtil.objectsHasNull(
                newPackMaintain.getSerialName(),
                newPackMaintain.getProductName(),
                newPackMaintain.getMaintainer(),
                newPackMaintain.getUpdateReason()
        ))
        {return Response.setResult(ResultCodeEnum.PARAM_LACK);}
        //检查是否与同意系列中已有产品名称冲突
        LambdaQueryWrapper<PackMaintain> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PackMaintain::getSerialName,newPackMaintain.getSerialName());
        wrapper.eq(PackMaintain::getProductName,newPackMaintain.getProductName());
        if (this.count(wrapper) > 0)
        {
            throw new SpiceException().message("该系列已有此产品包装维护信息");
        }
        this.save(newPackMaintain);
        return Response.ok().message("产品包装信息添加成功").data("data",newPackMaintain);
    }

    @Override
    public Response removePackMaintain(Long id) {
        //检查必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        }
        //确认要删除的是否存在
        PackMaintain removedPackMaintain = this.getById(id);
        if (CheckUtil.objectsHasNull(removedPackMaintain))
        {
            throw new SpiceException().message("待删除的产品包装维护信息不存在");
        }
        this.removeById(id);
        return Response.ok().message("产品包装信息删除成功").data("data",removedPackMaintain);
    }

    @Override
    public Response updatePackMaintain(Long id, PackMaintain newPackMaintain) {
    //确认必要参数
        if (CheckUtil.objectsHasNull(id,newPackMaintain))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认要更新的是否存在
        PackMaintain updatedPackMaintain = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedPackMaintain))
        {
            throw new SpiceException().message("待维护的产品包装维护数据不存在");
        }
        //更改后的产品名称是否与同系列的其他产品名称重名
        if (!updatedPackMaintain.getProductName().equals(newPackMaintain.getProductName()))
        {
            LambdaQueryWrapper<PackMaintain> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PackMaintain::getSerialName,newPackMaintain.getSerialName());
            wrapper.eq(PackMaintain::getProductName,newPackMaintain.getProductName());
            if (this.count(wrapper) > 0)
            {
                throw new SpiceException().message("该系列已有此产品包装维护信息");
            }
        }
        //更新待更新的数据id和createTime
        newPackMaintain.setId(id);
        newPackMaintain.setCreateTime(updatedPackMaintain.getCreateTime());
        if (this.updateById(newPackMaintain)) {
            return Response.ok().message("产品包装维护信息更新成功").data("data",newPackMaintain);
        }
        else {
            return Response.error().message("产品包装维护信息更新失败");
        }
    }
}
